package vanScheduling;

import db.TimeSlotDAO;
import java.util.List;
import java.sql.Time;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

@ManagedBean
@SessionScoped
public class TimeSlot {

    //Small inner class 
    //Extension of Time
    //toString changed to not display seconds
    //toString changed to display 12-hour clock
    private final class TimeDisp extends Time {

        public TimeDisp(Time time) {
            super(time.getTime());
        }

        public TimeDisp(int hr, int min) {
            super(hr, min, 0);
        }

        @Override
        public String toString() {
            int hour = super.getHours();
            int minute = super.getMinutes();
            String hourString;
            String minuteString;
            String ampm = "AM";

            //Determine AM or PM
            if (hour == 0) {hour+= 12;} //if 0-1, set am
            else if(hour == 12) {ampm = "PM";} //if 12, set pm
            else if (hour > 12) {ampm = "PM";hour -= 12;} //if >12, subtract 12 set am

            //Create String and return
            if (hour < 10) {
                hourString = "0" + hour;
            } else {
                hourString = Integer.toString(hour);
            }
            if (minute < 10) {
                minuteString = "0" + minute;
            } else {
                minuteString = Integer.toString(minute);
            }
            return (hourString + ":" + minuteString + " " + ampm);
        }
    }
    
    int id;
    int driverID;
    int startHr;
    int startMin;
    String startMer;
    int endHr;
    int endMin;
    String endMer;
    String weekday;
    TimeDisp startTime;
    TimeDisp endTime;
    String driverName;
    List<SelectItem> weekdays = new ArrayList<SelectItem>();

    //Creates weekdays list for display.
    public TimeSlot() {
        weekdays.add(new SelectItem("Monday", "Monday"));
        weekdays.add(new SelectItem("Tuesday", "Tuesday"));
        weekdays.add(new SelectItem("Wednesday", "Wednesday"));
        weekdays.add(new SelectItem("Thursday", "Thursday"));
        weekdays.add(new SelectItem("Friday", "Friday"));
        weekdays.add(new SelectItem("Saturday", "Saturday"));
        weekdays.add(new SelectItem("Sunday", "Sunday"));
    }

    //Used in retrieving TimeSlots
    public TimeSlot(int id, int driverID, String weekday, Time startTime, Time endTime, String driverName) {
        this.id = id;
        this.driverID = driverID;
        this.weekday = weekday;
        this.startTime = new TimeDisp(startTime);
        this.endTime = new TimeDisp(endTime);
        this.driverName = driverName;
    }

    //Not needed?
    /*public TimeSlot(int id, int driverID, String weekday,
    int startHr, int startMin, String startMer,
    int endHr, int endMin, String endMer,
    String driverName) {
    super();
    this.id = id;
    this.driverID = driverID;
    this.weekday = weekday;
    this.startHr = startHr; this.startMin = startMin; this.startMer = startMer;
    this.endHr = startHr; this.endMin = startMin; this.endMer = startMer;
    this.driverName = driverName;
    }*/

    public int getId() { //Primary Key, no setter
        return id;
    }
    public int getDriverID() { //Foreign Key, no setter
        return driverID;
    }

    public String getWeekday() {
        return weekday;
    }
    public void setWeekday(String day) {
        this.weekday = day;
    }

    public void setStartTime() {
        if (startMer.equals("PM") && startHr == 12){} //Do nothing
        else if(startMer.equals("PM")) {startHr += 12;} //If not 12, then add 12
        else if (startMer.equals("AM") && startHr == 12) {startHr -= 12;}//If 12 AM, subtract 12 when adding
        startTime = new TimeDisp(startHr, startMin);
    }
    public void setEndTime() {
        if (endMer.equals("PM") && endHr == 12){} //Do nothing
        else if(endMer.equals("PM")) {endHr += 12;} //If not 12 and PM, then add 12
        else if (endMer.equals("AM") && endHr == 12) {endHr -= 12;}//If 12 AM, subtract 12 when adding
        endTime = new TimeDisp(endHr, endMin);
    }

    public Time getStartTime() {
        return startTime;
    }
    public Time getEndTime() {
        return endTime;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setStartHr(int startHr) {
        this.startHr = startHr;
    }
    public void setStartMin(int startMin) {
        this.startMin = startMin;
    }
    public void setStartMer(String startMer) {
        this.startMer = startMer;
    }
    public void setEndHr(int endHr) {
        this.endHr = endHr;
    }
    public void setEndMin(int endMin) {
        this.endMin = endMin;
    }
    public void setEndMer(String endMer) {
        this.endMer = endMer;
    }
    
    public int getStartHr() {
        return startHr;
    }
    public int getStartMin() {
        return startMin;
    }
    public String getStartMer() {
        return startMer;
    }
    public int getEndHr() {
        return endHr;
    }
    public int getEndMin() {
        return endMin;
    }
    public String getEndMer() {
        return endMer;
    }

    public List<SelectItem> getWeekdays() {
        return weekdays;
    }

    public String addTimeSlot() {
        this.setStartTime();
        this.setEndTime();
        if (TimeSlotDAO.addTimeSlot(this)) {
            return "timeSlotList";
        } else {
            return "timeSlotAddFailure";
        }
    }

    public String deleteTimeSlot() {
        if(TimeSlotDAO.deleteTimeSlot(this)) {
            return "timeSlotList";
        } else {
            return "timeSlotDeleteFailure";
        }
    }

    public List<TimeSlot> getTimeSlots() {
        return TimeSlotDAO.getTimeSlots();
    }

    @Override
    public String toString()
    {
        return (weekday + ": From " + startTime.toString() + " To " + endTime.toString());
    }
}
